﻿//功能描述：绩效管理：项目考核
using Hong.IRepository.IRepository_AssessmentManagement;
using Hong.Model.SysDomain.Model_AssessmentManagement;
using Hong.Model.SysDomain.Model_AssessmentManagement.ViewAssClassAssSmaliProjec;
using Hong.Repository.Data;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using Hong.Common.Encryption;

namespace Hong.Repository.Repository.Repository_AssessmentManagement
{
    public class AssProjectAssessmen: IAssProjectAssessmen
    {
        #region 构造器注入
        private readonly T_HongShi_3Context _dal;
        //注入解密SQL连接字符串
        private readonly SecurityStatic _securityStatic;

        public AssProjectAssessmen(T_HongShi_3Context dal, SecurityStatic securityStatic)
        {
            _dal = dal;
            _securityStatic = securityStatic;
        }
        #endregion

      
        #region 项目考核
        #region 考核任务下拉
        /// <summary>
        /// 考核任务下拉
        /// </summary>
        /// <returns></returns>
        public List<AssessmentList> GetAssessmentList()
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select AssId from AssessmentList";
                var list = conn.Query<AssessmentList>(sql).ToList();
                return list;
            }
        }
        #endregion

        #region 显示查询考核项目
        /// <summary>
        /// 显示查询考核项目
        /// </summary>
        /// <param name="AssPrjectName"></param>
        /// <param name="AssId"></param>
        /// <returns></returns>
        public List<ViewAssPrjecSmali> GetViewAssPrjecSmali(string AssPrjectName, string AssId)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select * from AssessmentPrjec a  join AssSmaliProjec b on a.AssPrjectId = b.AssPrjectId join AssClass c on b.PrId=c.PrId join Dictionaries d on c.ClassId = d.dicId join AssessmentList e on a.AssPrjectListId = e.AssId ";
                if (!string.IsNullOrEmpty(AssPrjectName))
                {
                    sql += $" and a.AssPrjectName like '%{AssPrjectName}%'";
                }
                if (!string.IsNullOrEmpty(AssId))
                {
                    sql += $" and e.AssId='{AssId}'";
                }
                var list= conn.Query<ViewAssPrjecSmali>(sql);
                return list.ToList();
            }
        }
        #endregion

        #region 设置考核项目
        /// <summary>
        /// 设置考核项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateAssPrjecSmali(string id)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                string[] str = id.Split(',');
                foreach (var item in str)
                {
                       var sql = $"update AssessmentPrjec set AssStart=1 where AssPrjectId='{item}'";
                conn.Execute(sql);
                }
             
                return 1;
            }
        }
        #endregion

        #region 奖金调整
        /// <summary>
        /// 奖金调整
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateAssPrjec(string id)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"update AssessmentPrjec set AssbonusStart=1 where AssPrjectId='{id}'";
                conn.Execute(sql);
                return 1;
            }
        }
        #endregion


        #region 小考核项目
        #region 添加小考核项目
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="ass"></param>
        /// <returns></returns>
        public int AddAssSmaliProjec(AssSmaliProjec ass)
        {
            var i = _dal.AssSmaliProjecs.Add(ass);
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AssSmaliProjec GetAssSmaliProjecFT(int id)
        {
            var list = _dal.AssSmaliProjecs.FirstOrDefault
                (x => x.PrId == id);
            return list;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int PutAssSmaliProjec(AssSmaliProjec ass)
        {
            _dal.Entry(ass).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelAssSmaliProjec(int id)
        {
            var i = _dal.AssSmaliProjecs.Find(id);
            _dal.Remove(i);
            return _dal.SaveChanges();
        }
        /// <summary>
        /// 显示小项目考核
        /// </summary>
        /// <returns></returns>
        public List<ViewAssPrjecSmali> GetViewAssClassSmali(string ids)
        {
            var list2 = _dal.AssessmentPrjecs.ToList();
            var list = _dal.AssSmaliProjecs.ToList();
            list = list.Where(x => x.AssPrjectId == ids).ToList();
            var query = from a in list2
                        join b in list on a.AssPrjectId equals b.AssPrjectId
                        select new ViewAssPrjecSmali
                        {
                            AssPrjectId = a.AssPrjectId,
                            AssPrjectName = a.AssPrjectName,
                            AssPrjectListId = a.AssPrjectListId,
                            AssPrjectPartState = a.AssPrjectPartState,
                            PrId = b.PrId,
                            PrName = b.PrName,
                            PrPerformance = b.PrPerformance,
                            PrNote = b.PrNote,
                            PrFinished = b.PrFinished,
                            PrUnfinished = b.PrUnfinished,
                            PrHistory = b.PrHistory,
                            PrTotal = b.PrTotal,
                        };
            return query.ToList();
        }
        #endregion
        #endregion

        #region 考核分类
        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="ass"></param>
        /// <returns></returns>
        public int AddAssClass(AssClass ass)
        {
            var i = _dal.AssClasses.Add(ass);
            return _dal.SaveChanges();
        }
        #endregion

        #region 显示
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<ViewAssClasses> GetAssClass(int ids)
        {
            var list1 = _dal.AssClasses.Where(x => x.PrId == ids).ToList();
            var list2 = _dal.Dictionaries.ToList();
            var list = from a in list1
                       join b in list2 on a.ClassTypeId equals b.DicId
                       select new ViewAssClasses
                       {
                           ClassId = a.ClassId,
                           ClassTypeId = a.ClassTypeId,
                           ClassScale = a.ClassScale,
                           ClassDifficulty = a.ClassDifficulty,
                           ClassFictitious = a.ClassFictitious,
                           DicId = b.DicId,
                           DicName = b.DicName,
                           DicCode = b.DicCode,
                           DicDesc = b.DicDesc
                       };
            return list.ToList();
        }
        #endregion

        #region 反填
        /// <summary>
        /// 反填
        /// </summary>
        /// <returns></returns>
        public AssClass GetAssClassFT(int id)
        {
            var list = _dal.AssClasses.FirstOrDefault(x => x.ClassId == id);
            return list;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public int PutAssClass(AssClass ass)
        {
            _dal.Entry(ass).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _dal.SaveChanges();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int DelAssClass(int id)
        {
            var i = _dal.AssClasses.Find(id);
            _dal.Remove(i);
            return _dal.SaveChanges();
        }
        #endregion

        #endregion

        #endregion
    }
}
